package com.amigo.online.message.util;

import com.amigo.online.message.mybatisbean.MessageTbl;
import com.amigo.online.message.proto.IMBaseDefine;
import com.amigo.online.message.proto.IMMessage;
import com.google.protobuf.InvalidProtocolBufferException;

public class ChatMessageDecoder {
    public  MessageTbl decoder(short cid, byte[] bytes) throws InvalidProtocolBufferException {
        IMBaseDefine.MessageCmdID commandID=IMBaseDefine.MessageCmdID.forNumber(cid);
        switch (commandID){
            case CID_MSG_DATA:
                return parseMessage(IMMessage.IMMsgData.parseFrom(bytes));
        }
        return null;
    }

    private  MessageTbl parseMessage(IMMessage.IMMsgData data){
        MessageTbl message= new MessageTbl();
        if(data.getMsgType().getNumber()<0x06) { //单聊
            message.setSessionType(1);
        }else{//群聊
            message.setSessionType(2);
        }
        message.setFromUid((long) data.getFromUserId());
        message.setFromNickname(data.getFromUserNickname());
        message.setFromAvatar(data.getFromUserAvatar());
        message.setToUid((long) data.getToSessionId());
        message.setCreatedTime(System.currentTimeMillis());
        message.setLongitude(data.getLongitude());
        message.setLatitude(data.getLatitude());
        message.setContent(data.getMsgData().toStringUtf8());
        message.setMsgId(data.getMsgId());
        message.setDeleted(false);
        message.setPrevMsgId("");
        switch (data.getMsgType()){
            case MSG_TYPE_GROUP_TEXT:
            case MSG_TYPE_SINGLE_TEXT:
                message.setType((byte) 1);
                break;
            case MSG_TYPE_GROUP_IMAGE:
            case MSG_TYPE_SINGLE_IMAGE:
                message.setType((byte) 2);
                break;
            case MSG_TYPE_GROUP_AUDIO:
            case MSG_TYPE_SINGLE_AUDIO:
                message.setType((byte) 3);
                break;
            case MSG_TYPE_GROUP_VIDEO:
            case MSG_TYPE_SINGLE_VIDEO:
                message.setType((byte) 4);
                break;
            case MSG_TYPE_GROUP_LOCATION:
            case MSG_TYPE_SINGLE_LOCATION:
                message.setType((byte) 5);
                break;
        }
        return message;
    }
}
